package com.njwb.reflect;

import java.lang.reflect.Method;

/**
 * Description: 类的描述
 * Author: yuandeyin@itany.com
 * Date: 2025-08-07 14:19
 * Version: 1.0
 */
public class TestMethod {
    public static void main(String[] args) throws Exception {
        //正常的普通方法如何调用
//        Person person = new Person();
//        person.setName("小白");
//
//        System.out.println(person);
        //通过反射调用setName方法，给Person对象赋值
        //1.反射的核心是Class对象
        Class personClass = Class.forName("com.njwb.reflect.Person");

        //2.反射实例化对象
        Person person = (Person) personClass.newInstance();

        //3.获取setName方法
        Method setNameMethod = personClass.getDeclaredMethod("setName",String.class);
        //执行方法
        setNameMethod.invoke(person,"小黑");
        System.out.println(person);

    }
}
